<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\SmsConfig;
use App\Models\XnH5ThirdUser;
use App\Models\XnH5User;
use App\Services\SmsService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class SmsController extends Controller
{
    public function sendCode(Request $request)
    {
        $request->validate(['phone' => 'required|regex:/^1[3-9]\d{9}$/','type'=>'required']); // 1.外部用户 2.内部用户
        $code = rand(10000, 99999);
        $data = ['code'=>$code];
        $smsInfo = SmsConfig::query() -> where('state',1) -> where('type',$request->type) -> first();
        if(!$smsInfo){
            return fails('请配置短信');
        }
        // 调用短信服务（示例为阿里云）
        $success = SmsService::send($request->phone, $data,$smsInfo->signName,$smsInfo->templateCode,$request -> type);
        if ($success) {

            $user = XnH5User::query()->firstOrCreate(['phone' => $request->phone], [
                'password' => Hash::make(uniqid()),
                'type'=>$request->type,
            ]);
            $user -> sms_code = $code;
            $user -> expire_time = Carbon::now()->addHour()->timestamp;
            $user -> save();
            return successly();
        }
        return fails('发送失败');
    }
    public function test(){
        $success = SmsService::send(13607661635, [],'妙音悦','SMS_485470603',1);
        dump($success);
    }

    public function sendSmsRecharge(Request $request)
    {

        $request->validate(['phone' => 'required|regex:/^1[3-9]\d{9}$/']); // 1.外部用户 2.内部用户
        $smsInfo = SmsConfig::query() -> where('state',4) -> where('type',1) -> first();
        if(!$smsInfo){
            return fails('请配置短信');
        }
        // 调用短信服务（示例为阿里云）
        $success = SmsService::send($request->phone,[],$smsInfo->signName,$smsInfo->templateCode,1);
        if ($success) {
            return successly();
        }
        return fails('发送失败');
    }

    public function sendH5SmsCode(Request $request)
    {
        $request->validate(['phone' => 'required|regex:/^1[3-9]\d{9}$/','type'=>'required']); // 1.外部用户 2.内部用户
        $code = rand(10000, 99999);
        $data = ['code'=>$code];
        $smsInfo = SmsConfig::query() -> where('state',1) -> where('type',$request->type) -> first();
        if(!$smsInfo){
            return fails('请配置短信');
        }
        // 调用短信服务（示例为阿里云）
        $success = SmsService::send($request->phone, $data,$smsInfo->signName,$smsInfo->templateCode,$request -> type);
        if ($success) {
            $user = XnH5ThirdUser::query()->firstOrCreate(['phone' => $request->phone], [
                'password' => Hash::make(uniqid()),
                'type'=>$request->type,
            ]);
            $user -> sms_code = $code;
            $user -> expire_time = Carbon::now()->addHour()->timestamp;
            $user -> save();
            return successly();
        }
        return fails('发送失败');
    }
}
